package com.example.employeemanagement.service.impl;

import com.example.employeemanagement.dto.DepartmentSalaryDto;
import com.example.employeemanagement.dto.PerformanceTrendDto;
import com.example.employeemanagement.dto.PositionSalaryDto;
import com.example.employeemanagement.dto.SalaryRangeDto;
import com.example.employeemanagement.dto.dashboard.*;
import com.example.employeemanagement.mapper.DashboardMapper;
import com.example.employeemanagement.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 数据大屏服务实现类
 */
@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private DashboardMapper dashboardMapper;

    @Override
    public int getEmployeeCount() {
        return dashboardMapper.getEmployeeCount();
    }

    @Override
    public int getNewEmployeeCount() {
        return dashboardMapper.getNewEmployeeCount();
    }

    @Override
    public List<DepartmentDistributionDto> getDepartmentDistribution() {
        return dashboardMapper.getDepartmentDistribution();
    }

    @Override
    public List<PositionDistributionDto> getPositionDistribution() {
        return dashboardMapper.getPositionDistribution();
    }

    @Override
    public List<AttendanceStatusDto> getAttendanceStatus() {
        return dashboardMapper.getAttendanceStatus();
    }

    @Override
    public List<AttendanceTrendDto> getAttendanceTrend() {
        return dashboardMapper.getAttendanceTrend();
    }
    
    @Override
    public List<AttendanceTrendDto> getAttendanceTrend(String startDate, String endDate) {
        return dashboardMapper.getAttendanceTrendByDateRange(startDate, endDate);
    }

    @Override
    public List<AttendanceTrendDto> getDepartmentAttendance() {
        return dashboardMapper.getDepartmentAttendance();
    }

    @Override
    public PerformanceScoreDto getPerformanceScore() {
        // 由于Performance表可能没有足够的数据，这里模拟生成绩效分数分布数据
        
        // 创建一个PerformanceScoreDto对象，设置各个等级的人数
        PerformanceScoreDto dto = new PerformanceScoreDto();
        dto.setExcellent(45);  // 优秀(90-100分)人数
        dto.setGood(120);      // 良好(80-89分)人数
        dto.setAverage(68);    // 一般(70-79分)人数
        dto.setPoor(12);       // 较差(<70分)人数
        
        return dto;
    }

    @Override
    public List<DepartmentPerformanceDto> getDepartmentPerformance() {
        // 由于Performance表没有数据，这里模拟生成部门平均绩效数据
        List<DepartmentPerformanceDto> result = new ArrayList<>();
        
        // 添加模拟数据
        result.add(new DepartmentPerformanceDto(1, "研发部", 87.5));
        result.add(new DepartmentPerformanceDto(2, "市场部", 85.2));
        result.add(new DepartmentPerformanceDto(3, "人力资源部", 88.7));
        result.add(new DepartmentPerformanceDto(4, "技术部", 84.3));
        
        return result;
    }
    
    @Override
    public List<DepartmentPerformanceDto> getDepartmentPerformance(Integer year, Integer quarter) {
        // 由于Performance表没有数据，这里根据年份和季度模拟生成部门平均绩效数据
        List<DepartmentPerformanceDto> result = new ArrayList<>();
        
        // 检查季度参数是否有效
        if (quarter < 1 || quarter > 4) {
            return result; // 如果季度无效，返回空列表
        }
        
        // 根据不同的季度生成不同的模拟数据
        double baseScore = 80.0;
        double quarterFactor = quarter * 1.5; // 季度因子，使不同季度有不同的分数
        
        // 添加模拟数据
        result.add(new DepartmentPerformanceDto(1, "研发部", baseScore + quarterFactor + 2.5));
        result.add(new DepartmentPerformanceDto(2, "市场部", baseScore + quarterFactor - 1.3));
        result.add(new DepartmentPerformanceDto(3, "人力资源部", baseScore + quarterFactor + 3.2));
        result.add(new DepartmentPerformanceDto(4, "技术部", baseScore + quarterFactor - 2.1));
        
        return result;
    }

    @Override
    public List<PerformanceTrendDto> getPerformanceTrend() {
        // 由于Performance表可能没有足够的数据，这里模拟生成绩效趋势数据
        List<PerformanceTrendDto> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Random random = new Random();
        
        // 生成过去30天的绩效趋势数据
        for (int i = 30; i >= 0; i--) {
            Date date = new Date(System.currentTimeMillis() - i * 24 * 60 * 60 * 1000L);
            String dateStr = sdf.format(date);
            // 生成75-85之间的随机绩效分数
            double score = 75 + random.nextDouble() * 10;
            score = Math.round(score * 10) / 10.0; // 保留一位小数
            
            result.add(new PerformanceTrendDto(dateStr, score));
        }
        
        return result;
    }

    @Override
    public List<SalaryRangeDto> getSalaryDistribution() {
        return dashboardMapper.getSalaryDistribution();
    }

    @Override
    public List<DepartmentSalaryDto> getDepartmentSalary() {
        return dashboardMapper.getDepartmentSalary();
    }

    @Override
    public List<PositionSalaryDto> getPositionSalary() {
        return dashboardMapper.getPositionSalary();
    }
}